<template>
  <div class="login-container">
    <!-- 头部的 logo 区域 -->
    <div class="header">
      <img src="@/assets/images/logo.png" alt="" />
    </div>

    <!-- 登录和注册区域 -->
    <div class="box">
      <div class="box-header"></div>
      <!-- 登录的表单 -->
      <el-form :model="loginForm"  :rules="rule" ref="loginfromref">
              <!-- prop="要校验的字段名跟下面的usrname一样" -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
            placeholder="请输入用户名"
            maxlength="10"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
            placeholder="请输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="btn-login" @click="login">登录</el-button>
        <el-link type="info" class="link-reg" @click="$router.push('/reg')">去注册账号</el-link>
      </el-form>
    </div>
  </div>
</template>
<script>

// 配置基地址
import { reqLogin } from '@/api/user'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rule: {
        username: [
          // 非空校验
          { required: true, message: '用户名不能为空', trigger: ['blur', 'change'] },
          // 字段校验，长度校验 正则校验+自定义校验
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10位的字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          // 自定义校验
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符串', trigger: ['blur', 'change'] }
        ]
      }

    }
  },
  methods: {
    login () {
      // 整体表单校验   //t
      this.$refs.loginfromref.validate(async (flag) => {
        if (!flag) return console.log('校验未通过')

        console.log(reqLogin, 11111)
        const { data } = await reqLogin(this.loginForm)
        // loading.close()
        console.log(data)
        // 关闭loading

        // 3.请求处理（成功/失败）
        // 有结果可以关loadingl
        if (data.code !== 0) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        // （'键名'，真正的值）
        localStorage.setItem('cms-91', data.token)
        // 跳转页面 this.$router.push({pash:'/'})
        this.$router.push('/')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  background-color: blue;
  height: 100%;
  background: url('~@/assets/images/login_bg.jpg') no-repeat center;
  background-size: cover;

  .header {
    width: 1200px;
    margin: 0 auto;
    user-select: none;
  }

  .box {
    width: 400px;
    height: 270px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0 1px 15px rgba(0, 0, 0, 0.2);
    padding: 0 30px;
    box-sizing: border-box;

    .box-header {
      height: 60px;
      background: url('../../assets/images/login_title.png') no-repeat center;
    }

    .btn-login {
      width: 100%;
    }

    .link-reg {
      font-size: 12px;
    }
  }
}
</style>
